package o;

import androidx.annotation.Nullable;
import androidx.lifecycle.OnLifecycleEvent;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

public class kl {
    public static kl ˋ = new kl();
    private final Map<Class, e> ˊ = new HashMap();
    private final Map<Class, Boolean> ˎ = new HashMap();

    static class a {
        final Method ˊ;
        final int ॱ;

        a(int i, Method method) {
            this.ॱ = i;
            this.ˊ = method;
            this.ˊ.setAccessible(true);
        }

        void ˏ(kv kvVar, o.kp.e eVar, Object obj) {
            try {
                switch (this.ॱ) {
                    case 0:
                        this.ˊ.invoke(obj, new Object[0]);
                        return;
                    case 1:
                        this.ˊ.invoke(obj, new Object[]{kvVar});
                        return;
                    case 2:
                        this.ˊ.invoke(obj, new Object[]{kvVar, eVar});
                        return;
                    default:
                        return;
                }
            } catch (InvocationTargetException e) {
                throw new RuntimeException("Failed to call observer method", e.getCause());
            } catch (Throwable e2) {
                throw new RuntimeException(e2);
            }
        }

        public boolean equals(Object obj) {
            if (this == obj) {
                return true;
            }
            if (obj == null || getClass() != obj.getClass()) {
                return false;
            }
            a aVar = (a) obj;
            if (this.ॱ == aVar.ॱ && this.ˊ.getName().equals(aVar.ˊ.getName())) {
                return true;
            }
            return false;
        }

        public int hashCode() {
            return (this.ॱ * 31) + this.ˊ.getName().hashCode();
        }
    }

    public static class e {
        final Map<o.kp.e, List<a>> ˏ = new HashMap();
        final Map<a, o.kp.e> ॱ;

        e(Map<a, o.kp.e> map) {
            this.ॱ = map;
            for (Entry entry : map.entrySet()) {
                o.kp.e eVar = (o.kp.e) entry.getValue();
                List list = (List) this.ˏ.get(eVar);
                if (list == null) {
                    list = new ArrayList();
                    this.ˏ.put(eVar, list);
                }
                list.add(entry.getKey());
            }
        }

        public void ˏ(kv kvVar, o.kp.e eVar, Object obj) {
            ˋ((List) this.ˏ.get(eVar), kvVar, eVar, obj);
            ˋ((List) this.ˏ.get(o.kp.e.ON_ANY), kvVar, eVar, obj);
        }

        private static void ˋ(List<a> list, kv kvVar, o.kp.e eVar, Object obj) {
            if (list != null) {
                for (int size = list.size() - 1; size >= 0; size--) {
                    ((a) list.get(size)).ˏ(kvVar, eVar, obj);
                }
            }
        }
    }

    kl() {
    }

    boolean ॱ(Class cls) {
        Boolean bool = (Boolean) this.ˎ.get(cls);
        if (bool != null) {
            return bool.booleanValue();
        }
        Method[] ˋ = ˋ(cls);
        for (Method annotation : ˋ) {
            if (((OnLifecycleEvent) annotation.getAnnotation(OnLifecycleEvent.class)) != null) {
                ˋ(cls, ˋ);
                return true;
            }
        }
        this.ˎ.put(cls, Boolean.valueOf(false));
        return false;
    }

    private Method[] ˋ(Class cls) {
        try {
            return cls.getDeclaredMethods();
        } catch (Throwable e) {
            throw new IllegalArgumentException("The observer class has some methods that use newer APIs which are not available in the current OS version. Lifecycles cannot access even other methods so you should make sure that your observer classes only access framework classes that are available in your min API level OR use lifecycle:compiler annotation processor.", e);
        }
    }

    public e ˊ(Class cls) {
        e eVar = (e) this.ˊ.get(cls);
        if (eVar != null) {
            return eVar;
        }
        return ˋ(cls, null);
    }

    private void ॱ(Map<a, o.kp.e> map, a aVar, o.kp.e eVar, Class cls) {
        o.kp.e eVar2 = (o.kp.e) map.get(aVar);
        if (eVar2 != null && eVar != eVar2) {
            throw new IllegalArgumentException("Method " + aVar.ˊ.getName() + " in " + cls.getName() + " already declared with different @OnLifecycleEvent value: previous value " + eVar2 + ", new value " + eVar);
        } else if (eVar2 == null) {
            map.put(aVar, eVar);
        }
    }

    private e ˋ(Class cls, @Nullable Method[] methodArr) {
        e ˊ;
        Class superclass = cls.getSuperclass();
        Map hashMap = new HashMap();
        if (superclass != null) {
            ˊ = ˊ(superclass);
            if (ˊ != null) {
                hashMap.putAll(ˊ.ॱ);
            }
        }
        for (Class ˊ2 : cls.getInterfaces()) {
            for (Entry entry : ˊ(ˊ2).ॱ.entrySet()) {
                ॱ(hashMap, (a) entry.getKey(), (o.kp.e) entry.getValue(), cls);
            }
        }
        if (methodArr == null) {
            methodArr = ˋ(cls);
        }
        int length = methodArr.length;
        int i = 0;
        boolean z = false;
        while (i < length) {
            boolean z2;
            Method method = methodArr[i];
            OnLifecycleEvent onLifecycleEvent = (OnLifecycleEvent) method.getAnnotation(OnLifecycleEvent.class);
            if (onLifecycleEvent == null) {
                z2 = z;
            } else {
                int i2;
                Class[] parameterTypes = method.getParameterTypes();
                if (parameterTypes.length <= 0) {
                    i2 = 0;
                } else if (parameterTypes[0].isAssignableFrom(kv.class)) {
                    i2 = 1;
                } else {
                    throw new IllegalArgumentException("invalid parameter type. Must be one and instanceof LifecycleOwner");
                }
                o.kp.e value = onLifecycleEvent.value();
                if (parameterTypes.length > 1) {
                    if (!parameterTypes[1].isAssignableFrom(o.kp.e.class)) {
                        throw new IllegalArgumentException("invalid parameter type. second arg must be an event");
                    } else if (value != o.kp.e.ON_ANY) {
                        throw new IllegalArgumentException("Second arg is supported only for ON_ANY value");
                    } else {
                        i2 = 2;
                    }
                }
                if (parameterTypes.length > 2) {
                    throw new IllegalArgumentException("cannot have more than 2 params");
                }
                ॱ(hashMap, new a(i2, method), value, cls);
                z2 = true;
            }
            i++;
            z = z2;
        }
        ˊ = new e(hashMap);
        this.ˊ.put(cls, ˊ);
        this.ˎ.put(cls, Boolean.valueOf(z));
        return ˊ;
    }
}
